package com.example.demo.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Menu;
import com.example.demo.mapper.MenuMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/menumanager")
public class MenuController {

    @Resource
    MenuMapper menuMapper;

    @PostMapping
    public Result<?> save(@RequestBody Menu menu){
        menuMapper.insert(menu);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Menu menu){
        menuMapper.updateById(menu);
        return Result.success();
    }

    @DeleteMapping("/{foodid}")
    public Result<?> update(@PathVariable long foodid){
        menuMapper.deleteById(foodid);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Menu> wrapper = Wrappers.<Menu>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Menu::getFoodid, search);
        }
        Page<Menu> menuPage = menuMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(menuPage);
    }
}
